package com.itstu.exception;

import com.itstu.model.AjaxResult;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author &#x6881;&#x5353;
 * @Version 1.0
 * @ClassName MyExceptionHandler
 * @Description TODO
 * @Date 2024/09/27 21:38
 */
@RestControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public AjaxResult exceptionHandler(HttpServletRequest req, Exception e) {
        e.printStackTrace();
        if (e instanceof TokenExcption){
            return AjaxResult.error("token失效");
        }else if (e instanceof MalformedJwtException) {
            return AjaxResult.error("token格式错误");
        }else if (e instanceof ExpiredJwtException){
            return AjaxResult.error("token过期");
        } else if (e instanceof GlobalException) {
            return AjaxResult.error(e.getMessage());
        } else {
            return AjaxResult.error();
        }
    }
}
